/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package simplepushclient;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

/**
 *
 * @author Filip
 */
public class History{
    private File f;
    private ArrayList<String> log;

    public History() {
        this.f = new File(System.getenv("APPDATA"),"PushHistory.ini");
        getData();
    }

    private void getData() {
        if (f.exists()) {
            try{
                ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f));
                log = (ArrayList<String>) ois.readObject();
            }catch (Exception ex) {
                log = new ArrayList<String>();
            }
        } else {
            log = new ArrayList<String>();
        }
    }
    public ArrayList<String> refreshHistory(){
        getData();
        return log;
    }
    public ArrayList<String> getList(){
        return log;
    }
    public boolean saveHistory(){
        try{
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f));
            oos.writeObject(log);
            oos.close();
            return true;
        }catch (Exception ex) {
            System.err.println(ex);
            return false;
        }
    }
    public String getLatest(){
        if(log.isEmpty())
            return "";
        return log.get(log.size()-1).toString();
    }
    public void add(String s) {
        if(!log.contains(s)){
            log.add(s);
        }
    }

    protected void finalize() throws Throwable {
        saveHistory();
        super.finalize();
    }
}
